package yxy.game.pm2.command.player;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlayerMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.player.PlayerHandle;
import yxy.game.pm2.module.stat.StatHandle;

/** 玩家 玩家信息 */
@MessageType(proto = Command.PlayerInfo, classtype = PlayerMessage.PlayerInfoRequest.class)
public class CMD_PlayerInfo extends Command<PlayerMessage.PlayerInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, PlayerMessage.PlayerInfoRequest request) {
		String playerCode = request.getPlayerCode();

		PlayerMessage.PlayerInfoResponse.Builder response = PlayerMessage.PlayerInfoResponse.newBuilder();

		PlayerBean player = PlayerBean.get(playerCode);
		if (player == null) {
			send(messageSource, response, Result.PlayerNotExist);// 玩家不存在
			return;
		}

		//todo 修改登录时间，3天后自动清除缓存
		StatHandle.stat_用户登录状态更新(playerCode);

		//新版本竞技场 对战玩家的武将星级兼容
		PlayerMessage.PlayerInfo playerInfo = new PlayerHandle().playerInfo(player);
		HeroHandle.arenaChangeStar(playerInfo);

		try {
			response.setPlayerInfo(playerInfo);
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, PlayerMessage.PlayerInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}